<template>
  <!-- 表单 -->
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
    <el-form-item prop="username" label="姓名">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item prop="mobile" label="手机">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item prop="timeOfEntry" label="入职时间">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>

    <el-form-item prop="formOfEmployment" label="聘用形式">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option v-for="(obj, index) in formOfEmploymentList" :key="index" :label="obj.name" :value="obj.value" />
      </el-select>
    </el-form-item>

    <el-form-item prop="workNumber" label="工号">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item prop="departmentName" label="部门">
      <el-input
        v-model="formData.departmentName"
        style="width:50%"
        placeholder="请选择部门"
        @focus="departmentNameFocusFn"
      />
      <!-- 部门树状结构 -->
      <div v-show="showTree" class="tree-box">
        <el-tree :data="treeData" default-expand-all :props="{ label: 'name' }" @node-click="treeClickFn" />
      </div>
    </el-form-item>
    <el-form-item prop="correctionTime" label="转正时间">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelFn">取消</el-button>
      <el-button type="primary" @click="addSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { EmplyeesConst } from '@/api/constant/employees.js'
export default {
  name: 'EmpDialog',
  props: {
    treeData: {
      type: Array,
      default: _ => []
    }

  },
  data() {
    return {
    // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      showTree: false,
      // 添加表单的校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      // 聘用形式数组
      formOfEmploymentList: EmplyeesConst.formOfEmployment
    }
  },
  methods: {
    // 取消
    cancelFn() {
      this.$emit('close')
    },
    // 确定
    addSubmit() {
      // 兜底验证
      this.$refs.addForm.validate(valid => {
        if (!valid) return
        // 拷贝一份出来，避免置空表单时表单传给接口的是空对象
        this.$emit('add-emp', { ...this.formData })
        this.$emit('close')
      })
    },
    // 选择部门input获取焦点时的事件
    departmentNameFocusFn() {
      this.showTree = true
    },
    // 点击部门树事件
    treeClickFn(data) {
      // console.log(data)
      // 如果当前部门还有子部门，则不能被选中
      if (data && data.children) return
      // 把当前选中的节点显示在 input 框中
      this.formData.departmentName = data.name
      // 隐藏 tree
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
